package com.hean.activity;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import com.hean.util.Action;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;

public class Net extends Activity {
	private Button start;
	private ProgressBar pb;
	private EditText address;
	private DownloadAction action;
	private static final int MSG_PROGRESS = 1;

	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			if (msg.what == MSG_PROGRESS) {
				pb.setProgress(action.getProgress());
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.net);
		start = (Button) findViewById(R.id.net_start);
		pb = (ProgressBar) findViewById(R.id.net_pb);
		address = (EditText) findViewById(R.id.net_address);

		start.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String urlAddress = address.getText().toString();
				String fileName = "file" + System.currentTimeMillis();
				action = new DownloadAction(urlAddress, fileName);
				action.execute();
			}
		});

	}

	private class DownloadAction implements Action {
		private String urlAddress;
		private String fileName;
		private int progress;
		private int max;
		private InputStream is;
		private OutputStream os;

		DownloadAction(String urlAddress, String fileName) {
			this.urlAddress = urlAddress;
			this.fileName = fileName;
		}

		@Override
		public void execute() {
			try {
				urlAddress = "http://172.16.6.13:8080/dang/kexibushini.mp3";
				URL url = new URL(urlAddress);
				final HttpURLConnection con = (HttpURLConnection) url
						.openConnection();
//				con.setRequestMethod("GET");
//				if(con.getResponseCode()==200)
				//设置断点续传开始位置
				// con.setRequestProperty("RANGE", "bytes=1024");//在getInputStream之前才有用
				max = con.getContentLength();
				pb.setMax(max);
				System.out.println(max);
				//如果线程超过android的组件阻塞时间，就会被系统回收，因此使用线程
				new Thread() {
					public void run() {
						try {
							is = new BufferedInputStream(con.getInputStream());
							os = new BufferedOutputStream(new FileOutputStream(
									new File("/mnt/sdcard/", fileName)));

							int b = -1;
							progress = 0;
							while ((b = is.read()) != -1) {
								os.write(b);
								if (++progress % 100 == 0) {
									Message msg = new Message();
									msg.what = MSG_PROGRESS;
									handler.sendMessage(msg);
								}
							}
							os.close();
							is.close();
						} catch (Exception e) {
							throw new RuntimeException(e);
						}

					}
				}.start();

			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}

		public int getProgress() {
			return progress;
		}
	}
}
